<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'securityAlarm:alarmInfo:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    
    <div class="fireVeIm">
      <div class="fireImg">
        <span style="white-space: nowrap;">
          预警照片：
        </span>
        <Image v-if="warningImage" :src='warningImage' width="90%" height="90%" ></Image>
        <img v-else src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
      </div>

      <div class="fireImg">
        <span style="white-space: nowrap;">
          处理结果照片：
        </span>
        <Image v-if='normalImage' :src="normalImage" width="90%" height="90%"></Image>
        <img v-else src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
      </div>
    </div>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsSecurityAlarmAlarmInfoForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { AlarmInfo, alarmInfoSave, alarmInfoForm } from '/@/api/safealarm/safealarm';
  import { Image} from "ant-design-vue"

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('security_alarm.alarmInfo');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<AlarmInfo>({} as AlarmInfo);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增安全告警表') : t('编辑安全告警表'),
  }));
 
  const warningImage=ref('');
  const normalImage=ref('');
  const inputFormSchemas: FormSchema[] = [
    {
      label: t('安全预警标题'),
      field: 'alarmName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },  
    },
    {
      label: t('所属企业'),
      field: 'enterpriseName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('设备名称'),
      field: 'deviceName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('设备编码'),
      field: 'deviceCode',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('当前状态'),
      field: 'handleStatus',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('上报时间'),
      field: 'reportTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
        disabled: true
      },
    },
    // {
    //   label: t('预警图片'),
    //   field: 'warningImage',
    //   component: 'Input',
    // },
    // {
    //   label: t('恢复正常图片'),
    //   field: 'normalImage',
    //   component: 'Input',
    // },
    {
      label: t('处理人'),
      field: 'handlerName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
    {
      label: t('离岗时长'),
      field: 'leaveDuration',
      show:()=>record.value.alarmTypeCode==="Firemonitoring_room_unmanned",
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await alarmInfoForm(data);
    record.value = (res.alarmInfo || {}) as AlarmInfo;
    record.value.__t = new Date().getTime();
    warningImage.value=res.alarmInfo.warningImage||'';
    normalImage.value=res.alarmInfo.normalImage||'';
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      // console.log('submit', params, data, record);
      const res = await alarmInfoSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
<style scoped lang="less"> 
.fireVeIm{
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-bottom: 5%;
}
.fireImg {
  width: 40%;
  height: 200px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
</style>